// pages/classHomePage/group/groupPage/groupPage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    people: null,
    all_student_group: null,
    groupId: null,
    memberList: [],
    icon: {
      name: "arrowdown",
      size: 30
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      people: wx.getStorageSync("people")
    })
    if (options != null) {
      console.log('页面跳转传值：', options)
      this.setData({
        groupId: options['groupId']
      })
    }
    let _this = this
    _this.get_all_student_group().then(res => {
      console.log('进入页面加载all_student_group：', res);
    })
  },

  get_all_student_group: function () {
    let _this = this
    return new Promise(function (resolve, reject) {
      app.formGet('/get_all_student_group?teacher_group_id=' + _this.data.groupId).then(res => {
        console.log('获取一个分组类型老师添加的所有组all_student_group:', res);
        let all_student_group = res
        _this.setData({
          'all_student_group': all_student_group
        })
        resolve(res)
      })

    })
  },

  teacher_add_group_number: function () {
    let _this = this
    let teacher_group_id = _this.data.groupId
    wx.showModal({
      title: '添加分组',
      editable: true, //显示输入框
      placeholderText: '输入组名', //显示输入框提示信息
      success: res => {
        if (res.confirm) { //点击了确认
          console.log('正在添加分组：', res.content);
          let data = {
            'code': wx.getStorageSync("myCode"),
            'group_name': res.content,
            'teacher_account': wx.getStorageSync("account"),
            'teacher_group_id': teacher_group_id
          }
          app.formPost('/add_group_number', data).then(res => {
            console.log('添加分组结果：', res);
            _this.get_all_student_group()
          })

        } else {
          console.log('用户点击了取消')
        }
      }
    })

  },

  groupMember: function (e) {
    console.log(e);
    console.log('点击：', e.currentTarget.dataset['student_groupid']);
    let _this = this
    let student_groupId = e.currentTarget.dataset['student_groupid']
    _this.get_all_group_member(student_groupId).then(res => {
      console.log('get_all_group_member结果：', res);
      _this.setData({
        memberList: res
      })
    })
  },

  get_all_group_member: function (student_group_id) {
    return new Promise(function (resolve, reject) {
      app.formGet('/get_all_group_member?student_group_id=' + student_group_id).then(res => {
        console.log('get_all_group_member:', res);
        resolve(res)
      })
    })

  },

  //学生加入要用
  get_group_task_by_group_id: function (group_id) {
    let _this = this
    console.log('学生加入要用', group_id);
    return new Promise(function (resolve, reject) {
      let data = {
        'group_id': group_id
      }
      app.formPost('/get_group_task_by_group_id?', data).then(res => {
        console.log('get_group_task_by_group_id:', res);
        resolve(res)
      })

    })
  },

  studentJoinGroup: function (e) {
    let _this = this
    let student_groupId = e.target.dataset['student_groupid']
    let groupId = _this.data.groupId
    _this.get_group_task_by_group_id(groupId).then(res => {
      let groupSize = res['groupSize']
      let data = {
        'code': wx.getStorageSync("myCode"),
        'group_id': _this.data.groupId,
        'group_number': groupSize,
        'student_account': wx.getStorageSync("account"),
        'student_group_id': student_groupId,
        'student_id': app.globalData.student_info['id']
      }
      app.formPost('/student_add_group', data).then(res => {
        console.log('加入小组结果student_add_group：', res);
        _this.onLoad()
      })
    })

  },

  studentExitGroup: function (e) {
    let _this = this
    let student_groupId = e.target.dataset['student_groupid']
    let groupId = _this.data.groupId
    _this.get_group_task_by_group_id(groupId).then(res => {
      let data = {
        'code': wx.getStorageSync("myCode"),
        'group_id': _this.data.groupId,
        'student_account': wx.getStorageSync("account"),
        'student_group_id': student_groupId,
        'student_id': app.globalData.student_info['id']
      }
      console.log('exit:',data);
      app.formPost('/student_exit_group', data).then(res => {
        console.log('退出小组结果student_exit_group：', res);
        _this.onLoad()
      })
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})